/**
 * 
 */
package au.com.langdale.gwt.client.binding;

import au.com.langdale.gwt.client.binding.BindingManager.Binding;
import au.com.langdale.gwt.client.model.Property;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;

/**
 * Binds a TextBox, TextArea or other derivative of TextBoxBase to
 * a model and property.
 * 
 * @author adv
 *
 */
public class TextBoxBinding extends Binding implements ChangeListener {

	private TextBoxBase textBox;

	public TextBoxBinding(Object model, Property prop, TextBoxBase box) {
		super(model, prop);
		textBox = box;
		textBox.addChangeListener(this);
		refresh();
	}
	
	public void refresh() {
		textBox.setText(prop.get(model));	
	}
	
	public void onChange(Widget sender) {
		prop.set(model, textBox.getText());							
		fireChange();
	}
	
	public void destroy() {
		if( textBox != null) {
			textBox.removeChangeListener(this);
			textBox = null;
		}
	}

	public Widget getWidget() {
		return textBox;
	}
}